package ltd.dujiabao.springcloud.service.impl;

import ltd.dujiabao.springcloud.dao.OrderMapper;
import ltd.dujiabao.springcloud.domain.Order;
import ltd.dujiabao.springcloud.service.AccountFeignService;
import ltd.dujiabao.springcloud.service.OrderService;
import ltd.dujiabao.springcloud.service.StorageFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.seata.spring.annotation.GlobalTransactional;


/**
 * @author : DuJiabao
 * @Project : springcloud
 * @Time : 2021/5/26 20:54
 * @Desc :
 */
@Service
public class OrderServiceImpl implements OrderService {
  @Autowired
  AccountFeignService accountFeignService;
  @Autowired
  StorageFeignService storageFeignService;
  @Autowired
  OrderMapper orderMapper;

  @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
  @Override
  public int create(Order order) {
    orderMapper.create(order);
    storageFeignService.decrease(order.getProductId(), order.getCount());
    accountFeignService.decrease(order.getUserId(), order.getMoney());
    orderMapper.updateStatus(order.getUserId(), order.getStatus());
    return 0;
  }
}
